import { _decorator, Component, Node } from 'cc';
import { Core } from '../core/Core';
import { EGameEvent } from '../core/Config';
import { Context } from './Context';
import { BattleCamera } from './BattleCamera';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    @property(Node)
    nodeMenu: Node;
    @property(Node)
    nodeBattle: Node;

    protected onLoad(): void {
        //获取战斗场景的摄像机
        Context.battleCamera=this.node.getChildByName('BattleCamera').getComponent(BattleCamera);
    }

    protected onEnable(): void {
        Core.instance.event.on(EGameEvent.GAME_START,this.onGameStart,this);
        Core.instance.event.on(EGameEvent.GAME_END,this.onGameEnd,this);
        Core.instance.event.on(EGameEvent.SAVE,this.onGameSave,this);
        Core.instance.playerData.loginTime=new Date().getTime();
        Core.instance.event.emit(EGameEvent.SAVE);
        this.onGameEnd();//默认是退出游戏状态
    }
    protected onDisable(): void {
        Core.instance.event.off(EGameEvent.GAME_START,this.onGameStart,this);
        Core.instance.event.off(EGameEvent.GAME_END,this.onGameEnd,this);
        Core.instance.event.off(EGameEvent.SAVE,this.onGameSave,this);
    }

    start() {
        console.log('main start');
        //开始的时候让菜单可以用，战斗场景关闭
        this.nodeMenu.active=true;
        this.nodeBattle.active=false;

    }

    update(deltaTime: number) {

    }

    private onGameStart() {
        //开始游戏
        this.nodeMenu.active=false;
        this.nodeBattle.active=true;
    }
    private onGameEnd() {
        //退出游戏
        this.nodeMenu.active=true;
        this.nodeBattle.active=false;
    }
    private onGameSave() {
        //保存游戏
        Core.instance.save();
    }
}


